﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class FoodController : Controller
    {
        IFoodService _foodService;
        IRestaurantIngredientService _restaurantIngredientService;
        IFoodIngredientService _foodIngredientService;
        public FoodController()
        {
            _foodService = new FoodService();
            _restaurantIngredientService = new RestaurantIngredientService();
            _foodIngredientService = new FoodIngredientService();
        }


        // GET: RRShop/Food
        public ActionResult Index(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        [Authorize]
        public ActionResult List(int id)
        {
            ViewBag.Id = id;
            var foods = _foodService.FindList(10, "", id, 0, 0, null, null, 1);

            List<FoodViewModel> fvms = new List<FoodViewModel>();
            foreach(var food in foods)
            {
                fvms.Add(FoodViewModel.FromFood(food));
            }

            return View(fvms);
        }

        [Authorize]
        public ActionResult Add(int id)
        {
            ViewBag.Id = id;
            return View(new FoodViewModel() { Restaurant_Id = id });
        }

        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(FoodViewModel fvm, int id)
        {
            ViewBag.Id = id;
            if (ModelState.IsValid)
            {
                ViewBag.Id = id;
                var food = _foodService.Add(fvm.Food);
                if (food == null || food.Id <= 0)
                {
                    ModelState.AddModelError("", "添加失败");
                }
                else
                {
                    return RedirectToAction("List", new { Id = id });
                }
            }
         
            return View(fvm);
        }

        [Authorize]
        public ActionResult Edit(int id,int rid)
        {
            ViewBag.Id = rid;
            var food = _foodService.Find(id);
            return View(FoodViewModel.FromFood(food));
        }

        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(FoodViewModel fvm, int id, int rid)
        {
            ViewBag.Id = rid;
            if (ModelState.IsValid)
            {
                if (_foodService.Update(fvm.Food))
                {
                    ModelState.AddModelError("", "编辑成功");
                }
                else
                {
                    ModelState.AddModelError("", "编辑失败");
                }
            }
            return View(fvm);
        }

        [Authorize]
        public ActionResult Delete(int id, int rid)
        {
            ViewBag.Id = rid;
            var food = _foodService.Find(id);
            _foodService.Delete(food);

            return RedirectToAction("List", new { Id = rid });
        }

        [Authorize]
        public ActionResult Ingredient(int id, int rid)
        {
            ViewBag.FId = id;
            ViewBag.RId = rid;

            var ris = _restaurantIngredientService.FindList(100, "", rid, 1);
            var fis = _foodIngredientService.FindList(100, id, 1);
            List<FoodIngredientViewModel> fivms = new List<FoodIngredientViewModel>();
            foreach (var fi in fis)
            {
                fivms.Add(FoodIngredientViewModel.FromFoodIngredient(fi));
            }

            List<RestaurantIngredientViewModel> rivms = new List<RestaurantIngredientViewModel>();
            foreach (var ri in ris)
            {
                var rivm = RestaurantIngredientViewModel.FromRestaurantIngredient(ri);
                var ff = fivms.FirstOrDefault(f => f.Ingredient_Id == ri.Ingredient_Id);
                rivm.IsFood =  ff!= null;
                rivm.Amount = ff != null ? ff.Amount : 0;
                rivms.Add(rivm);
            }


            return View(rivms);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AddIngredient(FoodIngredientViewModel fi)
        {
            RespMessage<string> rm = new RespMessage<string>();

            var foodIngredient = _foodIngredientService.FindByIngredient(fi.Food_Id, fi.Ingredient_Id);
            if (foodIngredient != null)
            {
                foodIngredient.Amount = fi.Amount;
                foodIngredient.Ingredient_Id = fi.Ingredient_Id;
                foodIngredient.Food_Id = fi.Food_Id;
                rm.Success = _foodIngredientService.Update(foodIngredient);
            }
            else
            {
                var a = _foodIngredientService.Add(fi.FoodIngredient);
                if (a != null && a.Id > 0)
                    rm.Success = true;
                else
                    rm.Success = false;
            }

            return Json(rm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult DeleteIngredient(FoodIngredientViewModel fi)
        {
            RespMessage<string> rm = new RespMessage<string>();
            var foodIngredient = _foodIngredientService.FindByIngredient(fi.Food_Id, fi.Ingredient_Id);
            if (foodIngredient == null)
                rm.Success = true;
            else
                rm.Success = _foodIngredientService.Delete(foodIngredient);
            return Json(rm);
        }
    }
}